/**
 * Copyright(C) 2012 Luvina
 * MstJapanDaoImpl.java, 05-06-2012 NguyenVietDung
 */
package net.luvina.manageuser.dao.impl;

import java.sql.SQLException;
import java.util.List;

import net.luvina.manageuser.dao.MstJapanDao;
import net.luvina.manageuser.hibernate.MstJapan;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

/**
 * ClassDescription
 *
 * @author NguyenVietDung
 *
 */
@Repository("mstJapanDao")
public class MstJapanDaoImpl extends GenericDaoImpl<MstJapan, Integer> implements MstJapanDao {

	/**
	 * MethodDescrition
	 *
	 * @param type
	 */
	public MstJapanDaoImpl() {
		super(MstJapan.class);
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * net.luvina.manageuser.dao.MstJapanDao#existMstJapan(java.lang.String)
	 */
	@Override
	public boolean existMstJapan(final String codeLevel) {
		HibernateCallback callback = new HibernateCallback() {
			/*
			 * (non-Javadoc)
			 *
			 * @see
			 * org.springframework.orm.hibernate3.HibernateCallback#doInHibernate
			 * (org.hibernate.Session)
			 */
			@Override
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				StringBuilder sqlCommand = new StringBuilder();
				sqlCommand.append(" FROM MstJapan jp ");
				sqlCommand.append(" WHERE jp.codeLevel=:codeLevel");
				Query query = session.createQuery(sqlCommand.toString());
				query.setString("codeLevel", codeLevel);
				List<MstJapan> lsMstJapan = (List<MstJapan>) query.list();
				System.out.println("||||||" + lsMstJapan.size());
				boolean isExist = false;
				if (lsMstJapan.size() == 0) {
					isExist = false;
				} else if (lsMstJapan.size() > 0) {
					isExist = true;
				}
				return isExist;
			}
		};
		return getHibernateTemplate().execute(callback);
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see net.luvina.manageuser.dao.MstJapanDao#getListJapan()
	 */
	@Override
	public List<MstJapan> getListJapan() {
		HibernateCallback callback = new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session) throws HibernateException, SQLException {

				// StringBuilder sqlCommand = new StringBuilder();
				// sqlCommand.append(" FROM MstJapan ");
				// Query query = session.createQuery(sqlCommand.toString());
				// List<MstJapan> lsMstJapan = query.list();
				// return lsMstJapan;
				StringBuilder sqlCommand = new StringBuilder();
				sqlCommand.append(" SELECT ");
				sqlCommand.append(" code_level as codeLevel, ");
				sqlCommand.append(" name_level as nameLevel ");
				sqlCommand.append(" FROM mst_japan ");
				Query query = session.createSQLQuery(sqlCommand.toString()).addScalar("codeLevel", Hibernate.STRING).addScalar("nameLevel",
						Hibernate.STRING).setResultTransformer(Transformers.aliasToBean(MstJapan.class));
				List<MstJapan> lsMstJapan = query.list();
				return lsMstJapan;
			}

		};
		return (List<MstJapan>) getHibernateTemplate().execute(callback);
	}

}
